package df.bean.process;

import df.bean.db.conn.DBConnection;
import df.bean.db.table.BankTMBMediaClearing;
import df.bean.db.table.Doctor;
import df.bean.db.table.IntErpArReceipt;
import df.bean.db.table.IntHisBill;
import df.bean.db.table.IntHisDc;
import df.bean.db.table.IntHisVerify;
import df.bean.db.table.PaymentMonthly;
import df.bean.db.table.PayorOffice;
import df.bean.db.table.SummaryMonthly;
import df.bean.db.table.SummaryMonthlyStep;
import df.bean.db.table.SummaryTax406;
import df.bean.db.table.TrnDaily;
import df.bean.obj.receipt.Receipt;
import df.bean.obj.util.DialogBox;


public class ProcessRollBack {
    private DBConnection conn = null;
    
    
    public ProcessRollBack(DBConnection conn) {
        this.conn = conn;
    }
    // TrnDaily
    public boolean rollBackTrnDaily(String hospitalCode, String startDate, String endDate) {
        boolean result = true;
        TrnDaily td = new TrnDaily(conn);

        try {
            conn.beginTrans();
            if (result) { result = td.rollBackUpdate(hospitalCode, startDate, endDate);  }
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
            
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            td = null;
        }
        
        return result;    
    }
    
    // Summary_Monthly
    public boolean rollBackSummaryMonthly(String hospitalCode, String YYYY, String MM) {
        boolean result = true;
        SummaryMonthly sm = new SummaryMonthly(conn);
        SummaryMonthlyStep st = new SummaryMonthlyStep(conn);

        try {
            conn.beginTrans();
            if (result) { result = sm.rollBackDelete(hospitalCode,YYYY, MM);  }
            if (result) { result = st.rollBackDelete(hospitalCode,YYYY, MM);  }            
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            sm = null;
            st = null;
        }
        
        return result;
    }
    
    // Payment_Monthly
    public boolean rollBackPaymentMonthly(String hospitalCode, String YYYY, String MM, String paymentType) {
        boolean result = true;
        PaymentMonthly pm = new PaymentMonthly(conn);

        try {
            conn.beginTrans();
            if (result) { result = pm.rollBackDelete(hospitalCode,YYYY, MM, paymentType);  }          
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            pm = null;
        }
        
        return result;    
    }
    // Media clearing
    public boolean rollBackMediaClearing(String hospitalCode, String YYYY, String MM, String serviceType) {
        boolean result = true;
        BankTMBMediaClearing tmb = new BankTMBMediaClearing(conn);

        try {
            conn.beginTrans();
            if (result) { result = tmb.rollBackDelete(hospitalCode,YYYY, MM, serviceType);  }          
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            tmb = null;
        }
        
        return result;    
    }
    // Summary Tax 406
    public boolean rollBackSummaryTax406(String hospitalCode, String YYYY, String MM) {
        boolean result = true;
        SummaryTax406 st406 = new SummaryTax406(conn);

        try {
            conn.beginTrans();
            if (result) { result = st406.rollBackDelete(hospitalCode, YYYY, MM);  }          
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            st406 = null;
        }
        
        return result;    
    }
    //////////////////////////////////////////////////////////////
    // roll back receipt
    public boolean rollBackReceipt(String hospitalCode, String YYYY, String MM) {
        boolean result = true;
        Receipt rc = new Receipt(conn);
        PayorOffice st = new PayorOffice(conn);
        IntErpArReceipt er = new IntErpArReceipt(conn);
        Doctor dt = new Doctor(conn);
        
 
        try {
            conn.beginTrans();
            if (result) { result = rc.rollBackUpdate(hospitalCode,YYYY, MM, "TRN_DAILY");  }
            if (result) { result = st.rollBackUpdate(hospitalCode,YYYY, MM, "TRN_DAILY");  }
            if (result) { result = er.rollBackUpdate(hospitalCode,YYYY, MM, "TRN_DAILY");  }
            if (result) { result = dt.rollBackUpdate(hospitalCode,YYYY, MM, "TRN_DAILY");  }
            
//            if (result) { result = rc.rollBackUpdate(hospitalCode,YYYY, MM, "SUMMARY_DAILY");  }
//            if (result) { result = st.rollBackUpdate(hospitalCode,YYYY, MM, "SUMMARY_DAILY");  }
//            if (result) { result = er.rollBackUpdate(hospitalCode,YYYY, MM, "SUMMARY_DAILY");  }
//            if (result) { result = dt.rollBackUpdate(hospitalCode,YYYY, MM, "SUMMARY_DAILY");  }
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            rc = null;
            st = null;
            er = null;
            dt = null;
        }
        
        return result;    
    }
        
    // roll back receipt by AR
    public boolean rollBackReceiptByAR(String hospitalCode, String YYYY, String MM) {
        boolean result = true;
        IntErpArReceipt er = new IntErpArReceipt(conn);
        
        try {
            conn.beginTrans();
            if (result) { result = er.rollBackUpdate(hospitalCode,YYYY, MM, "TRN_DAILY");  }
            
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            er = null;
        }
        
        return result;    
    }
    // roll back receipt by Admission
    public boolean rollBackReceiptByAdmission(String hospitalCode, String YYYY, String MM) {
        boolean result = true;
        IntErpArReceipt st = new IntErpArReceipt(conn);
 
        try {
            conn.beginTrans();
            if (result) { result = st.rollBackAdmissionTypeUpdate(hospitalCode,YYYY.concat(MM).concat("01"), 
            		YYYY.concat(MM).concat("31"));  }
            
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            st = null;
        }
        
        return result;    
    }     
    // roll back receipt by Payor
    public boolean rollBackReceiptByPayor(String hospitalCode, String YYYY, String MM) {
        boolean result = true;
        PayorOffice st = new PayorOffice(conn);
 
        try {
            conn.beginTrans();
            if (result) { result = st.rollBackUpdate(YYYY, MM,hospitalCode,"TRN_DAILY");  }
            
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            st = null;
        }
        
        return result;    
    }
    
    // roll back receipt by Payor
    public boolean rollBackReceiptByDoctor(String hospitalCode, String YYYY, String MM) {
        boolean result = true;
        Doctor dt = new Doctor(conn);
        
 
        try {
            conn.beginTrans();
            if (result) { result = dt.rollBackUpdate(hospitalCode,YYYY, MM, "TRN_DAILY");  }
            
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            dt = null;
        }
        
        return result;    
    }
    
    /////////////////////////////////////////////////////////
    
    // roll back import bill
    public boolean rollBackImportBill(String hospitalCode, String startDate, String endDate) {
        boolean result = true;
        TrnDaily td = new TrnDaily(conn);

        try {
            conn.beginTrans();
            if (result) { 
            	result = td.rollBackImportVerifyDelete(hospitalCode,startDate, endDate);
            	result = td.rollBackImportBillDelete(hospitalCode,startDate, endDate);  
            }
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            td = null;
        }
        
        return result;    
    }
    
    // roll back import bill by verify
    public boolean rollBackImportVerify(String hospitalCode, String startDate, String endDate) {
        boolean result = true;
        TrnDaily td = new TrnDaily(conn);

        try {
            conn.beginTrans();
            if (result) { result = td.rollBackImportVerifyDelete(hospitalCode,startDate, endDate);  }
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            td = null;
        }
        
        return result;    
    }
    
    // roll back interface INT_HIS_BILL
    public boolean rollBackInterfaceHisBill(String hospitalCode, String startDate, String endDate) {
        boolean result = true;
        IntHisBill ihb = new IntHisBill(conn);

        try {
            conn.beginTrans();
            if (result) { result = ihb.rollBackDelete(hospitalCode,startDate, endDate);  }
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            ihb = null;
        }
        
        return result;    
    }
    // roll back interface INT_HIS_VERIFY
    public boolean rollBackInterfaceHisVerify(String hospitalCode, String startDate, String endDate) {
        boolean result = true;
        IntHisVerify ihv = new IntHisVerify(conn);

        try {
            conn.beginTrans();
            if (result) { result = ihv.rollBackDelete(hospitalCode,startDate, endDate);  }
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            ihv = null;
        }
        
        return result;    
    }    
    // roll back interface INT_ERP_AR_RECEIPT
    public boolean rollBackInterfaceErpArReceipt(String hospitalCode, String startDate, String endDate) {
        boolean result = true;
        IntErpArReceipt ar = new IntErpArReceipt(conn);

        try {
            conn.beginTrans();
            if (result) { result = ar.rollBackDelete(hospitalCode,startDate, endDate);  }
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            ar = null;
        }
        
        return result;    
    }
    
    // roll back interface Write off
    public boolean rollBackWriteOff(String hospitalCode, String startDate, String endDate) {
        boolean result = true;
        IntErpArReceipt ar = new IntErpArReceipt(conn);

        try {
            conn.beginTrans();
            if (result) { result = ar.rollBackWriteOffDelete(hospitalCode, startDate, endDate);  }
        } catch (Exception ex) {
            //DialogBox.ShowError("Error : " + ex.getMessage());
        } finally {
            if (result) { conn.commitTrans(); }
            if (!result) { conn.rollBackTrans(); }
            ar = null;
        }
        
        return result;    
    }
    public boolean rollBackInterfaceDisContinue(String hospitalCode, String startDate, String endDate) {
    	boolean result = true;
    	IntHisDc dc = new IntHisDc(conn);
    	try {
    		conn.beginTrans();
    		if (result) { result = dc.rollBackDelete(hospitalCode,startDate, endDate);  }
    	} catch (Exception ex) {
    		
    		//DialogBox.ShowError("Error : " + ex.getMessage());
    	} finally {
    		if (result) { conn.commitTrans(); }
    		if (!result) { conn.rollBackTrans(); }
    		dc = null;
    	}
    	return result;    
    }  
    
    public boolean rollBackDisContinue(String hospitalCode, String startDate, String endDate) {
    	boolean result = true;
    	IntHisDc dc = new IntHisDc(conn);
    	try {
    		conn.beginTrans();
    		if (result) { result = dc.rollBackUpdateTrn(hospitalCode,startDate, endDate);  }
    	} catch (Exception ex) {
    		//DialogBox.ShowError("Error : " + ex.getMessage());
    	} finally {
    		if (result) { conn.commitTrans(); }
    		if (!result) { conn.rollBackTrans(); }
    		dc = null;
    	}
    	return result;    
    }
    
    
}
